package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import com.changxianggu.cloud.crawler.core.utils.ExportExcelUtil;
import com.changxianggu.cloud.crawler.core.utils.HttpUtil;
import com.changxianggu.cloud.crawler.core.utils.UserAgentUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 高等教育出版社配套资源
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class GaojiaoResource {

    @Autowired
    ICxgBookService bookService;

    public static final String COOKIE = "acw_tc=8cf9e2d216215658994112218e3726d1f7b8507a874bd5a13e37ebeeb9; SESSION=52836521-d892-472b-b20b-22ab21b328b7; Hm_lvt_fbec258952e854d110faf4241b905d55=1621565900; Hm_lpvt_fbec258952e854d110faf4241b905d55=1621565900; JSESSIONID=60C9023A020C85E6DCCCBD3E0810A4F9";
    public static final String ORIGIN = "";
    public static final String REFERER = "";
    public static final String URL_PRE = "";

    @Test
    public void initCrawler() throws Exception {
        doCrawler();
    }

    public void doCrawler() throws Exception {
        int pageNum = 1;
        int totalPage = 290;
        List<Map<String, String>> newList = new ArrayList<>();
        for (int i = pageNum; i <= totalPage; i++) {
            String url = "http://xuanshu.hep.com.cn/front/book/findBookProducts";
            Map<String, Object> parameter = MapUtil.newHashMap(12);
            parameter.put("pageNum", i);
            parameter.put("hasMatchingResource", "yes");
            Map<String, Object> headers = MapUtil.newHashMap(5);
            headers.put("Cookie", COOKIE);
            headers.put("User-Agent", UserAgentUtil.random());
            String result = HttpUtil.doPost(url, headers, parameter);
            if (StrUtil.isNotBlank(result)) {
                Document htmlDoc = Jsoup.parse(result);
                Element bookContent = htmlDoc.select("body").first();
                if (bookContent != null) {
                    Elements elements = bookContent.select(".book-info");
                    if (elements != null && elements.size() > 0) {
                        for (Element ele : elements) {
                            Map<String, String> map = new HashMap<>();
                            String href = ele.select("a").attr("href");
                            String id = href.split("bookId=")[1];
                            String title = ele.select(".title").text();
                            map.put("id", id);
                            map.put("title", title);
                            newList.add(map);
                        }
                    }
                    log.info("第" + i + "页数据保存成功");
                    log.warn("--->" + i);
                }
            }
        }
        //修改后的list 然后导出
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("dataList", newList);
        Workbook workbook = ExportExcelUtil.exportExcel("/mapper/template/excel/resourceExcelTest.xlsx", dataMap);
        OutputStream os = new FileOutputStream("F:/" + System.currentTimeMillis() + ".xls");
        workbook.write(os);
    }

    /**
     * 判断一个字符串是否都为数字
     *
     * @param strNum
     * @return
     */
    public boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * 截取数字
     *
     * @param content
     * @return
     */
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
